package com.atguigu.guli.serviceedu.controller;

import com.atguigu.guli.serviceedu.entity.Course;
import com.atguigu.guli.serviceedu.entity.vo.ChapterVo;
import com.atguigu.guli.serviceedu.entity.vo.CourseQueryVo;
import com.atguigu.guli.serviceedu.entity.vo.CourseWebVo;
import com.atguigu.guli.serviceedu.service.ChapterService;
import com.atguigu.guli.serviceedu.service.CourseService;
import com.atguigu.servicebase.result.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "前台课程查询")
@CrossOrigin
@RestController
@RequestMapping("/edu/course")
public class CourseApiController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    //查询前8条热门课程
    @Cacheable(value = "course-index", key = "'IndexList'")
    @GetMapping("index")
    public R index() {
        //查询前8条热门课程
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<Course> courseList = courseService.list(wrapper);

        return R.ok().data("courseList",courseList);
    }


    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true, example = "1")
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true, example = "8")
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
        @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<Course> pageParam = new Page<Course>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }


    @ApiOperation(value = "根据ID查询课程")
    @GetMapping(value = "{courseId}")
    public R getById(
            @ApiParam(name = "courseId", value = "课程ID", required = true, example = "1")
            @PathVariable String courseId){
        //查询课程信息和讲师信息
        CourseWebVo courseWebVo = courseService.selectInfoWebById(courseId);
        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);
        return R.ok().data("course", courseWebVo).data("chapterVoList", chapterVoList);
    }
}
